package org.qs.webservice.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.qs.webservice.dao.content.ConBookMapper;
import org.qs.webservice.dao.content.ConBookStatMapper;
import org.qs.webservice.dao.contentWrite.WriteConBookStatMapper;
import org.qs.webservice.dao.user.UsBookReviewMapper;
import org.qs.webservice.dao.user.UsBookReviewReplyMapper;
import org.qs.webservice.dao.user.UsMemberPointMapper;
import org.qs.webservice.dao.user.UsUserMapper;
import org.qs.webservice.dao.user.UserGradeMapper;
import org.qs.webservice.domain.ConBook;
import org.qs.webservice.domain.ConBookStat;
import org.qs.webservice.domain.UsBookReview;
import org.qs.webservice.domain.UsBookReviewReply;
import org.qs.webservice.domain.UsMemberPoint;
import org.qs.webservice.domain.UsUser;
import org.qs.webservice.exception.QSReadException;
import org.qs.webservice.service.BookReviewService;
import org.qs.webservice.service.IreadMemcacheService;
import org.qs.webservice.service.UpdateDataService;
import org.qs.webservice.util.DateUtil;
import org.qs.webservice.util.IReadUtils;
import org.qs.webservice.util.StringUtils;
import org.qs.webservice.vo.BookReview;
import org.qs.webservice.vo.BookReviewReply;
import org.qs.webservice.vo.PageData;
import org.springframework.stereotype.Service;

@Service
public class BookReviewServiceImpl implements BookReviewService {
	@Resource
	private UsBookReviewMapper usBookReviewMapper;
	@Resource
	private UsBookReviewReplyMapper usBookReviewReplyMapper;
	@Resource
	private ConBookStatMapper conBookStatMapper;
	@Resource
	private ConBookMapper conBookMapper;
	@Resource
	private WriteConBookStatMapper writeConBookStatMapper;
	@Resource
	private IreadMemcacheService ireadMemcacheService;
	@Resource
	private UsUserMapper usUserMapper;
	@Resource
	private UpdateDataService updateDataService;
	@Resource
	private UserGradeMapper userGradeMapper;
	@Resource
	private UsMemberPointMapper usMemberPointMapper;
	@Override
	public String getReviewList(int review_type, int book_id, int essence,
			int pages, int contents, String order,int chapter_id) {
		PageData<BookReview> pageData = new PageData<BookReview>();
		pageData.setCode(200);
		pageData.setPageNumber(pages);
		pageData.setPageSize(contents);
		int index=(pages-1)*contents;
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		if(book_id==0||pages==0||contents==0){
			tokenMap.put("code", 403);
			tokenMap.put("reason","参数错误");
			return IReadUtils.beanToJson(tokenMap); 
		}
		
		ConBookStat conBookStat = conBookStatMapper.getBookStatById(book_id);
		if(conBookStat==null){
			ConBookStat new_conBookStat = new ConBookStat();
			updateDataService.updateConBookStat(conBookStat, book_id,4,0,new_conBookStat,0);
			conBookStat = new_conBookStat;
		}
		List<UsBookReview> usBookReviews = new ArrayList<UsBookReview>();
		if(review_type==0&&essence==0){	
			pageData.setTotal(usBookReviewMapper.getBookReviewCountByList(book_id, -1, essence,0));
			usBookReviews= usBookReviewMapper.getBookReviewByList(book_id, -1, essence, index, contents, chapter_id);
		}else{
			pageData.setTotal(usBookReviewMapper.getBookReviewCountByList(book_id, review_type, essence,0));
			usBookReviews= usBookReviewMapper.getBookReviewByList(book_id, review_type, essence, index, contents, chapter_id);
		}
		
		List<BookReview> list = new ArrayList<BookReview>();
		for(UsBookReview usBookReview:usBookReviews){
			BookReview bookReview = new BookReview();
			bookReview.setContent_id(usBookReview.getReview_id()+"");
			bookReview.setContent(usBookReview.getContent());
			bookReview.setCreate_time(DateUtil.getString(usBookReview.getCreate_time(), "yyyy-MM-dd HH:mm:ss"));
			bookReview.setUser_level(IReadUtils.getUserLevel(ireadMemcacheService,usBookReview.getPoint(),userGradeMapper));
			if(usBookReview.getEssence()==1){
				bookReview.setEssence("[精华]");
			}else{
				bookReview.setEssence("");
			}
		
			bookReview.setTitle(usBookReview.getTitle());
			if(usBookReview.getTop()==1){
				bookReview.setTop("[置顶]");
			}else{
				bookReview.setTop("");
			}
			bookReview.setUser_id(usBookReview.getUser_id()+"");
			bookReview.setUser_name(usBookReview.getUser_name());
			if(usBookReview.getUser_id()==0L){
				bookReview.setUser_name("游客");
			}
			List<BookReviewReply> replys = new ArrayList<BookReviewReply>();
			for(UsBookReviewReply usBookReviewReply:usBookReview.getReplys()){
				BookReviewReply reply = new BookReviewReply();
				if(usBookReviewReply.getReply_id()==0)continue;
				reply.setContent_id(usBookReviewReply.getReply_id()+"");
				reply.setContent(usBookReviewReply.getContent());
				reply.setCreate_time(DateUtil.getString(usBookReviewReply.getCreate_time(), "yyyy-MM-dd HH:mm:ss"));
				reply.setUser_id(usBookReviewReply.getUser_id()+"");
				reply.setUser_name(usBookReviewReply.getUser_name());
				if(usBookReviewReply.getUser_id()==0L){
					reply.setUser_name("游客");
				}
				if(usBookReviewReply.getUser_id().equals(conBookStat.getUser_id()))
					reply.setIs_author(1);
				replys.add(reply);
			}
			bookReview.setReply_cnt(replys.size());
			bookReview.setReplys(replys);
			bookReview.setIamge_url(usBookReview.getImage_url());
			if(StringUtils.isNullOrEmpty(usBookReview.getImage_url())){
				bookReview.setIamge_url("");
			}
			if(conBookStat.getUser_id().equals(usBookReview.getUser_id()))
				bookReview.setIs_author(1);
			list.add(bookReview);
		}
		pageData.setContent(list);
		return IReadUtils.beanToJson(pageData); 
	}
	
	@Override
	public String addReview(String ip, int book_id, int chap_id, String title,
			String content, int type,String token,String device_identifier) throws Exception {
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		if(book_id==0||StringUtils.isNullOrEmpty(title)||StringUtils.isNullOrEmpty(content)){
			tokenMap.put("code", 403);
			tokenMap.put("reason","参数错误");
			return IReadUtils.beanToJson(tokenMap); 
		}
		if(type==2&&chap_id==0){
			tokenMap.put("code", 403);
			tokenMap.put("reason","参数错误");
			return IReadUtils.beanToJson(tokenMap); 
		}
		String user_id = IReadUtils.getUserIdByToken(token,ireadMemcacheService,device_identifier);
		if(StringUtils.isNullOrEmpty(user_id)){
			tokenMap.put("code", 403);
			tokenMap.put("reason","用户未登录");
			return IReadUtils.beanToJson(tokenMap); 
		}
		UsUser usUser = usUserMapper.getCustomUserByUserid(Long.valueOf(user_id));
		if(usUser==null){
			tokenMap.put("code", 403);
			tokenMap.put("reason","用户不存在");
			return IReadUtils.beanToJson(tokenMap); 
		}
		if(ireadMemcacheService.getBookReview(user_id+book_id)!=null){
			tokenMap.put("code", 403);
			tokenMap.put("reason","请不要频繁评论");
			return IReadUtils.beanToJson(tokenMap); 
		}
		int review_type=0;
		//章节评论
		if(type==2){
			review_type=1;
		}
		UsBookReview usBookReview = new UsBookReview();
		usBookReview.setBook_id(book_id);
		usBookReview.setChap_id(chap_id);
		usBookReview.setContent(content);
		usBookReview.setTitle(title);
		usBookReview.setReview_type(review_type);
		usBookReview.setUser_id(usUser.getUser_id());
		usBookReview.setReply_cnt(0);
		usBookReview.setStatus(1);
		usBookReview.setUser_ip(ip);
		usBookReview.setUser_name(usUser.getUser_name());
		int ret = usBookReviewMapper.addReview(usBookReview);
		//if(review_type==0){
			ConBookStat conBookStat = conBookStatMapper.getBookStatById(book_id);
			if(ret ==1){
				if(!updateDataService.updateConBookStat(conBookStat, book_id, 4,1,null,0)){
					throw new Exception("添加评论异常");
				}
		//	}
			
		}
		
		
		tokenMap.put("code", 200);
		tokenMap.put("content","评论成功");
		ireadMemcacheService.setetBookReview(user_id+book_id, "1");
		
		try {
			UsMemberPoint usMemberPoint =usMemberPointMapper.getPoint(Long.valueOf(user_id));
			if(usMemberPoint!=null){
				usMemberPoint.setPoint(usMemberPoint.getPoint()+1);
			}else{
			}
			updateDataService.updateMemberPoint(usMemberPoint,0,1,Long.valueOf(user_id));
			
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String, Object>();
			errorJson.put("user_id", usUser.getUser_id());
			QSReadException.printf(e, "添加积分异常");
		}
		
		
		return IReadUtils.beanToJson(tokenMap); 
	}
	@Override
	public String addReply(String ip,int review_id,String  content,String token,String device_identifier) {
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		if(review_id==0||StringUtils.isNullOrEmpty(content)){
			tokenMap.put("code", 403);
			tokenMap.put("reason","参数错误");
			return IReadUtils.beanToJson(tokenMap); 
		}
		String user_id = IReadUtils.getUserIdByToken(token,ireadMemcacheService,device_identifier);
		if(StringUtils.isNullOrEmpty(user_id)){
			tokenMap.put("code", 403);
			tokenMap.put("reason","用户未登录");
			return IReadUtils.beanToJson(tokenMap); 
		}
		UsUser usUser = usUserMapper.getCustomUserByUserid(Long.valueOf(user_id));
		if(content.equals((String)ireadMemcacheService.getBookReviewReply(user_id+review_id))){
			tokenMap.put("code", 403);
			tokenMap.put("reason","请不要回复相同内容");
			return IReadUtils.beanToJson(tokenMap); 
		}
		UsBookReviewReply usBookReviewReply = new UsBookReviewReply();
		usBookReviewReply.setReview_id(review_id);
		usBookReviewReply.setContent(content);
		usBookReviewReply.setUser_id(usUser.getUser_id());
		usBookReviewReply.setStatus(1);
		usBookReviewReply.setUser_ip(ip);
		usBookReviewReply.setUser_name(usUser.getUser_name());
		usBookReviewReplyMapper.addReviewReply(usBookReviewReply);
		usBookReviewMapper.updateReviewNum(review_id,1);
		
		
		tokenMap.put("code", 200);
		tokenMap.put("content","回复成功");
		ireadMemcacheService.setetBookReviewReply(user_id+review_id, content);
		return IReadUtils.beanToJson(tokenMap); 
	}
	@Override
	public String deleteReview(int review_id, String token,
			String device_identifier) throws Exception {
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		if(review_id==0){
			tokenMap.put("code", 403);
			tokenMap.put("reason","参数错误");
			return IReadUtils.beanToJson(tokenMap); 
		}
		String user_id = IReadUtils.getUserIdByToken(token, ireadMemcacheService, device_identifier);
		if(StringUtils.isNullOrEmpty(user_id)){
			tokenMap.put("code", 403);
			tokenMap.put("reason","用户未登录");
			return IReadUtils.beanToJson(tokenMap); 
		}
		UsBookReview usBookReview = usBookReviewMapper.getReviewById(review_id);
		if(null!=usBookReview)
			if(Long.valueOf(user_id).equals(usBookReview.getUser_id())){
				int ret = usBookReviewMapper.deleteReview(review_id);
				if(ret ==1/*&&usBookReview.getReview_type()==0*/){
					ConBookStat conBookStat = conBookStatMapper.getBookStatById(usBookReview.getBook_id());
					if(!updateDataService.updateConBookStat(conBookStat, usBookReview.getBook_id(), 4, -1,null,0)){
						throw new Exception("删除评论异常");
					}
				}
				tokenMap.put("code", 200);
				tokenMap.put("content","删除成功");
				return IReadUtils.beanToJson(tokenMap); 
			}else{
				ConBook conBook = conBookMapper.getBookById(usBookReview.getBook_id());
				if(null!=conBook)
					if(Long.valueOf(user_id).equals(conBook.getAuthor_id())){
						int ret = usBookReviewMapper.deleteReview(review_id);
						if(ret ==1/*&&usBookReview.getReview_type()==0*/){
							ConBookStat conBookStat = conBookStatMapper.getBookStatById(usBookReview.getBook_id());
							if(!updateDataService.updateConBookStat(conBookStat, usBookReview.getBook_id(), 4, -1,null,0)){
								throw new Exception("删除评论异常");
							}
						}
					
						tokenMap.put("code", 200);
						tokenMap.put("content","删除成功");
						return IReadUtils.beanToJson(tokenMap); 
					}
			}
		tokenMap.put("code", 403);
		tokenMap.put("reason","无权限删除");
		return IReadUtils.beanToJson(tokenMap); 
		
	}
	@Override
	public String deleteReply(int reply_id, String token,
			String device_identifier) {
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		if(reply_id==0){
			tokenMap.put("code", 403);
			tokenMap.put("reason","参数错误");
			return IReadUtils.beanToJson(tokenMap); 
		}
		String user_id = IReadUtils.getUserIdByToken(token, ireadMemcacheService, device_identifier);
		if(StringUtils.isNullOrEmpty(user_id)){
			tokenMap.put("code", 403);
			tokenMap.put("reason","用户未登录");
			return IReadUtils.beanToJson(tokenMap); 
		}
		UsBookReviewReply usBookReviewReply = usBookReviewReplyMapper.getReviewReply(reply_id);
		if(null!=usBookReviewReply)
			if(Long.valueOf(user_id).equals(usBookReviewReply.getUser_id())){
				usBookReviewReplyMapper.deleteReviewReply(reply_id);
				tokenMap.put("code", 200);
				tokenMap.put("content","删除成功");
				return IReadUtils.beanToJson(tokenMap); 
			}else{
				ConBook conBook = conBookMapper.getBookById(usBookReviewReply.getBook_id());
				if(null!=conBook)
					if(Long.valueOf(user_id).equals(conBook.getAuthor_id())){
						int ret = usBookReviewMapper.deleteReview(reply_id);
						if(ret==1){
							for(int i=0;i<10;i++){
								usBookReviewMapper.updateReviewEssence(usBookReviewReply.getReview_id(),-1);
							}
						}
					
						
						
						tokenMap.put("code", 200);
						tokenMap.put("content","删除成功");
						return IReadUtils.beanToJson(tokenMap); 
					}
			}
		tokenMap.put("code", 403);
		tokenMap.put("reason","无权限删除");
		return IReadUtils.beanToJson(tokenMap); 
	}
	@Override
	public String updateTop(int review_id, String token,
			String device_identifier, int value) {
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		if(review_id==0){
			tokenMap.put("code", 403);
			tokenMap.put("reason","参数错误");
			return IReadUtils.beanToJson(tokenMap); 
		}
		String user_id = IReadUtils.getUserIdByToken(token, ireadMemcacheService, device_identifier);
		if(StringUtils.isNullOrEmpty(user_id)){
			tokenMap.put("code", 403);
			tokenMap.put("reason","用户未登录");
			return IReadUtils.beanToJson(tokenMap); 
		}
		UsBookReview usBookReview = usBookReviewMapper.getReviewById(review_id);
		if(null!=usBookReview){
			ConBook conBook = conBookMapper.getBookById(usBookReview.getBook_id());
			if(null!=conBook)
				if(Long.valueOf(user_id).equals(conBook.getAuthor_id())){
					usBookReviewMapper.updateReviewTop(review_id,value);
					tokenMap.put("code", 200);
					if(value==1)
						tokenMap.put("content","置顶成功");
					else
						tokenMap.put("content","取消置顶成功");
					return IReadUtils.beanToJson(tokenMap); 
				}
		}
		tokenMap.put("code", 403);
		if(value==1)
			tokenMap.put("reason","无权限置顶");
		else
			tokenMap.put("reason","无权限取消置顶");
		return IReadUtils.beanToJson(tokenMap); 
	}
	@Override
	public String updateEssence(int review_id, String token,
			String device_identifier, int value) {
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		if(review_id==0){
			tokenMap.put("code", 403);
			tokenMap.put("reason","参数错误");
			return IReadUtils.beanToJson(tokenMap); 
		}
		String user_id = IReadUtils.getUserIdByToken(token, ireadMemcacheService, device_identifier);
		if(StringUtils.isNullOrEmpty(user_id)){
			tokenMap.put("code", 403);
			tokenMap.put("reason","用户未登录");
			return IReadUtils.beanToJson(tokenMap); 
		}
		UsBookReview usBookReview = usBookReviewMapper.getReviewById(review_id);
		if(null!=usBookReview){
			ConBook conBook = conBookMapper.getBookById(usBookReview.getBook_id());
			if(null!=conBook)
				if(Long.valueOf(user_id).equals(conBook.getAuthor_id())){
					usBookReviewMapper.updateReviewEssence(review_id,value);
					tokenMap.put("code", 200);
					if(value==1)
						tokenMap.put("content","加精成功");
					else
						tokenMap.put("content","取消加精成功");
					return IReadUtils.beanToJson(tokenMap); 
				}
		}
		tokenMap.put("code", 403);
		if(value==1)
			tokenMap.put("reason","无权限加精");
		else
			tokenMap.put("reason","无权限取消加精");
		return IReadUtils.beanToJson(tokenMap); 
	}
	@Override
	public String getReviewListByUser( int pages, int contents, String order,String device_identifier ,String token) {
		if(pages==0){
			pages=1;
		}
		if(contents==0){
			contents=20;
		}
		
		PageData<BookReview> pageData = new PageData<BookReview>();
		pageData.setCode(200);
		pageData.setPageNumber(pages);
		pageData.setPageSize(contents);
		int index=(pages-1)*contents;
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		String user_id = IReadUtils.getUserIdByToken(token, ireadMemcacheService, device_identifier);
		if(StringUtils.isNullOrEmpty(user_id)){
			tokenMap.put("code", 403);
			tokenMap.put("reason","用户未登录");
			return IReadUtils.beanToJson(tokenMap); 
		}
		pageData.setTotal(usBookReviewMapper.getBookReviewListByUserNum(Long.valueOf(user_id)));
		List<UsBookReview> usBookReviews = usBookReviewMapper.getBookReviewListByUser(Long.valueOf(user_id), index, contents);
		List<BookReview> list = new ArrayList<BookReview>();
		for(UsBookReview usBookReview:usBookReviews){
			BookReview bookReview = new BookReview();
			bookReview.setContent_id(usBookReview.getReview_id()+"");
			bookReview.setContent(usBookReview.getContent());
			bookReview.setBook_id(usBookReview.getBook_id()+"");
			bookReview.setBook_name(usBookReview.getBook_name());
			bookReview.setCreate_time(DateUtil.getString(usBookReview.getCreate_time(), "yyyy-MM-dd HH:mm:ss"));
			if(usBookReview.getEssence()==1){
				bookReview.setEssence("[精华]");
			}else{
				bookReview.setEssence("");
			}
		/*	bookReview.setReply_cnt(usBookReview.getReplys().size());*/
			bookReview.setTitle(usBookReview.getTitle());
			if(usBookReview.getTop()==1){
				bookReview.setTop("[置顶]");
			}else{
				bookReview.setTop("");
			}
			bookReview.setUser_id(usBookReview.getUser_id()+"");
			bookReview.setUser_name(usBookReview.getUser_name());
			List<BookReviewReply> replys = new ArrayList<BookReviewReply>();
			for(UsBookReviewReply usBookReviewReply:usBookReview.getReplys()){
				BookReviewReply reply = new BookReviewReply();
				if(usBookReviewReply.getReply_id()==0)continue;
				reply.setContent_id(usBookReviewReply.getReply_id()+"");
				reply.setContent(usBookReviewReply.getContent());
				reply.setCreate_time(DateUtil.getString(usBookReviewReply.getCreate_time(), "yyyy-MM-dd HH:mm:ss"));
				reply.setUser_id(usBookReviewReply.getUser_id()+"");
				reply.setUser_name(usBookReviewReply.getUser_name());
				if(usBookReviewReply.getUser_id().equals(usBookReview.getAuthor_id()))
					reply.setIs_author(1);
				replys.add(reply);
			}
			bookReview.setReply_cnt(replys.size());
			bookReview.setReplys(replys);
			bookReview.setIamge_url(usBookReview.getImage_url());
			if(StringUtils.isNullOrEmpty(usBookReview.getImage_url())){
				bookReview.setIamge_url("");
			}
			if(usBookReview.getUser_id().equals(usBookReview.getAuthor_id()))
				bookReview.setIs_author(1);
			list.add(bookReview);
		}
		pageData.setContent(list);
		return IReadUtils.beanToJson(pageData); 
	}
}
